package com.yiwenup.leetcode.site;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.site
 * @Author yiwenup
 * @Date 2022-04-17 11:42:24
 * @Description https://leetcode-cn.com/problems/most-common-word/
 */
public class No0819 {
    /**
     * 执行用时：7 ms, 在所有 Java 提交中击败了62.50%的用户
     * 内存消耗：40.9 MB, 在所有 Java 提交中击败了81.83%的用户
     */
    public String mostCommonWord(String paragraph, String[] banned) {
        Map<String, Integer> map = new HashMap<>();
        paragraph = paragraph.toLowerCase();
        for (int i = 0; i < paragraph.length(); i++) {
            if (!Character.isLetter(paragraph.charAt(i))) {
                continue;
            }
            int j = i;
            while (j < paragraph.length() && Character.isLetter(paragraph.charAt(j))) {
                j++;
            }
            String s = paragraph.substring(i, j);
            map.put(s, map.getOrDefault(s, 0) + 1);
            i = j;
        }
        for (String word : banned) {
            map.remove(word);
        }
        String ans = null;
        for (String s : map.keySet()) {
            if (map.get(s) > map.getOrDefault(ans, 0)) {
                ans = s;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new No0819().mostCommonWord("Bob", new String[0]));
    }
}
